package cn.edu.tju.scs.leave.service.impl;

import cn.edu.tju.scs.leave.dao.MenuDao;
import cn.edu.tju.scs.leave.dao.RoleDao;
import cn.edu.tju.scs.leave.entity.Menu;
import cn.edu.tju.scs.leave.entity.Role;
import cn.edu.tju.scs.leave.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by haoxiaotian on 2016/9/4 20:17.
 */

@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private MenuDao menuDao;

    @Override
    public List<Menu> queryMenusByUserId(int id) {
        List<Role> roles = roleDao.queryRoleByUserId(id);
        List<Menu> firstLevel = menuDao.queryFirstLevelMenus(roles);
        List<Menu> secondMenu = menuDao.querySecondLevelMenus(roles);


        for(Menu menu:firstLevel){
            menu.setSeconds(getByParentId(secondMenu,menu.getId()));
        }

        return firstLevel;
    }

    List<Menu> getByParentId(List<Menu> menus,int id){
        List<Menu> result = new ArrayList<>();
        for(Menu menu:menus){
            if(menu.getParentId() == id){
                result.add(menu);
            }
        }
        return result;
    }
}
